package cn.ac.iie.kgmanager.domain;

import cn.ac.iie.kgmanager.exception.ParamException;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * @ClassName Node
 * @Author tjh
 * @Date 19/6/12 上午10:12
 * @Version 1.0
 **/
@Data
public class Node {
    private String id;
    private String name;
    private Set<String> labels;
    private Map<String, Object> properties;

    public void addLabels(String... labels) {
        if(this.labels == null) {
            this.labels = new HashSet<>();
        }

        this.labels.addAll(Arrays.asList(labels));
    }

    public void addProperty(String key, Object value) {
        if(this.properties == null) {
            this.properties = new HashMap<>();
        }

        this.properties.put(key, value);
    }

    public void addProperties(Map<String, Object> properties) {
        if(this.properties == null) {
            this.properties = new HashMap<>();
        }

        this.properties.putAll(properties);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        Node node = (Node) o;
        return Objects.equals(id, node.id) &&
                Objects.equals(name, node.name) &&
                Objects.equals(labels, node.labels);
    }

    @Override
    public int hashCode() {

        return Objects.hash(super.hashCode(), id, name, labels);
    }

    public void check() throws ParamException {
        if(StringUtils.isBlank(this.name)) {
            throw new ParamException("节点name字段不能为空");
        }

        if(this.labels == null || this.labels.isEmpty()) {
            throw new ParamException("节点labels字段不能为空");
        }
    }
}
